**********************************************************************
*Create scatterplot data 
**********************************************************************
	*File options 
	local is is 
	local N 500 

*I. 1st stage bootstrapped hospital-level regressions 
	use "$reg\FULLsample`is'", clear	
	*CONTROL VARS 
	{
	local hc2 bdtot teach mcare mcaid type_gac
	local area rural white college unempl poverty elderly state_exp_status 	
	local pc female age utilm90 
	local plan fullyinsured
	local hospcontrol `hc2' `area' hhi
	local ptntcontrol `pc' `plan' 
	local control never 
	local treat (incl_1`is')
	local fe i.year
	}

	*Bootstrap samples
	{
	capture drop exptot paytot fte 
	ren pbfexptot exptot 
	ren pbfpaytot paytot
	ren pbffte fte 
	distinct num if incl_1`is'==1
	gegen numgrp = group(num) if incl_1`is'==1 & wt_cemm > 0 
	gdistinct numgrp 
	local max = r(ndistinct)
	drop if wt_cemm==0 
	
	*Bootstrap samples 
	*Sample providers N times, stratified by control status
	set seed 1234 
	forval i = 1/`N' {
	gen bwt_`i' = 0 
	bsample, cluster(num_prv) weight(bwt_`i') strata(never)
	}
	qui compress 
	
	*Gen matching weights for each sample
	merge 1:1 num_prv ye using "$reg\fulldata_wts`is'", keepusing(cem_strata)
	drop if _m==2
	drop _m 
	
	forval i = 1/`N' {
	*Drop treated if missing all control hospitals in strata
	qui gen tn = never & bwt_`i' > 0 
	qui bysort cem_strata: gegen ctn = sum(tn)	
	qui replace bwt_`i' = 0 if ctn==0 
	ren bwt_`i' temp 
	qui bysort num_: gegen bwt_`i' = min(temp)	
	count if temp!=bwt_`i'
	drop ct? t? temp
	
	*Drop control hospitals if missing all treateds in all strata 
	qui gen ti = incl_1`is' & bwt_`i' > 0 
	qui gen temp = cem_strata 
	qui bysort cem_strata: gegen cti = sum(ti)	
	qui replace temp = . if cti==0
	qui bysort num_: gegen ct = count(temp)
	replace bwt_`i' = 0 if never & ct==0
	qui drop ti temp ct*
	}
	
	forval i = 1/`N' {
	qui replace bwt_`i' = bwt_`i' * wt_cemm
	qui replace bwt_`i' = round(bwt_`i' * 100,1)
	}	
	
	*Save weights 
	preserve
	keep num_ ye bwt_*
	save "$reg\scatter_bweights", replace
	restore
	}
	
	*Prepare hospital-level vars  
	{
	ren num_ num 
	sort num ye 
	xtset num ye 
	
	*Pre-merger values
	foreach var in cost fte exptot paytot {
	gen dv_`var' = . 
	forval i = 1/3 {
	qui gen t = `var' if pre`i'_1`is' & wt_cemm > 0 
	qui bysort num: gegen m = mean(t)
	qui replace dv_`var' = m if missing(dv_`var')
	drop t m 
	}
	}
	
	foreach var in cost fte exptot paytot {
	qui gen _dv`var' = .
	forval i = 1/`max' {
	qui sum dv_`var' if numgrp==`i'
	qui replace _dv`var' = r(mean) in `i'
	}
	}	
	drop dv*
	
	gen _num = .
	forval i = 1/`max' {
	qui sum num if numgrp==`i', d
	qui replace _num = r(max) in `i'
	}
	}
	
	*Bootstrapped regressions
	{
	gen bwt_0 = wt_cemm 
	qui compress 
	forval j = 0/`N' {
	foreach var in cost fte exptot paytot {
	if mod(`j',10)==0 {
	di "***`var' `j'***" 
	}
	
	if `j'==0 {
	local wts "[aw=wt_cemm]" 
	}
	else {
	local wts  [fw=bwt_`j']
	}
	qui gen _b`var'`j' = .
	
	if "`var'"=="cost" {
	local ptntcontrol `pc' `plan' 
	local drg tot_drgwt
	}
	else {
	local ptntcontrol 
	local drg 
	}
	
	forval i = 1/`max' {
	qui count if `treat' & numgrp==`i' & bwt_`j' > 0
	if r(N)==0 {	
	}
	
	else {	
	qui xtreg `var' bought_1`is' `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' & numgrp==`i') | `control' `wts', fe vce(cluster num) 
	matrix a = e(b)
	if `i'==1 {
	di a[1,1]
	}
	qui replace _b`var'`j' = a[1,1] in `i'
	}
	}
	}
	}
	
	local b _b*
	unab b: `b'
	foreach var in `b' {
	replace `var' = . if `var'==0
	}
	drop if missing(_num)
	keep _* vol
	count
	save "$reg\temp_effects", replace
	}
	
*II. 1st stage bootstrapped patient-level regressions 
	*Note: takes > 1hr to run
	use "$reg\cardiosample`is'", clear
	*CONTROL VARS 
	{
	local plan plan* indiv* fullyinsured
	local pc female age elix_ inp_util_c 
	local ptntcontrol `pc' `plan'
	local drg tot_drgwt
	local fe i.year i.num_prv
	}
	
	*Bootstrap samples
	{
	ren p_read90_inp read 
	local var read 
	drop if missing(read)
	
	*Drop if unmatched 
	gen t1 = bought_1`is'  & wt_cemm > 0 
	gen t2 = !bought_1`is' & wt_cemm > 0 
	bysort num: gegen ct1 = sum(t1) 
	bysort num: gegen ct2 = sum(t2) 
	drop if (ct1==0 | ct2==0) & incl_1`is'==1
	drop ct* t?
	
	gegen numgrp = group(num) if incl_1`is'==1 & wt_cemm > 0 & !missing(wt_cemm)
	gdistinct numgrp 
	local max = r(ndistinct)
	
	*Merge to bootstrap weights
	drop if wt_cemm==0
	merge m:1 num_ ye using "$reg\scatter_bweights", keepusing(bwt_*)
	drop if _m==2
	drop _m 
	ren num_ num 
	}
	
	*Prepare hospital-level vars  
	{
	*Pre-merger values
	sort idcode indexd 
	gen dv_`var' = . 
	forval i = 1/3 {
	qui gen t = `var' if pre`i'_1`is' & wt_cemm > 0 
	qui bysort num: gegen m = mean(t)
	qui replace dv_`var' = m if missing(dv_`var')
	drop t m 
	}
	
	qui gen _dv`var' = .
	forval i = 1/`max' {
	qui sum dv_`var' if numgrp==`i'
	qui replace _dv`var' = r(mean) in `i'
	}
	drop dv*
	
	gen _num = .
	forval i = 1/`max' {
	qui sum num if numgrp==`i', d
	qui replace _num = r(max) in `i'
	}
	}
	
	*Bootstrapped regressions
	{
	gen bwt_0 = wt_cemm
	qui compress 
	forval j = 0/`N' {
	di "***`j'***" 
	
	if `j'==0 {
	local wts "[aw=wt_cemm]" 
	}
	else {
	local wts  [fw=bwt_`j']
	}
	qui gen _b`var'`j' = .
	
	forval i = 1/`max' {
	qui count if `treat' & numgrp==`i' & bwt_`j' > 0
	if r(N)==0 {	
	}
	
	else {	
	qui reghdfe `var' bought_1`is' `ptntcontrol' `hospcontrol' `drg' if (`treat' & numgrp==`i') | `control' `wts', absorb(i.year i.num) vce(cluster num) 
	matrix a = e(b)
	if `i'==1 {
	di a[1,1]
	}
	qui replace _b`var'`j' = a[1,1] in `i'
	}
	}
	}
	
	local b _b*
	unab b: `b'
	foreach var in `b' {
	replace `var' = . if `var'==0
	}
	
	keep _* 
	drop if missing(_num)
	count	
	save "$reg\temp_reffects", replace
	}
	
	*Save input data 
	{
	use "$reg\temp_reffects", clear
	merge 1:1 _num using "$reg\temp_effects"
	drop _m
	count 
	qui compress
	save "$sdb\ploteffects", replace
	}
